//注册全局过滤器 - 转换货币格式
Vue.filter('currency' , (money)=>{
   return parseFloat(money).toFixed(2)
})

let app = new Vue({
    el:"#app",
    data:{
        cartData:[//购物车数据
            {
                id: 20200303,
                title: "日韩流行风时尚美眉最爱独特米字拼图金属坡跟公主靴子黑色",
                saler: "纤巧百媚",
                shop: "纤巧百媚时尚鞋坊卖家",
                imgUrl: "image/taobao_cart_01.jpg",
                point: 5,
                price: 138,
                num: 1,
                isChecked: false
            },
            {
                id: 20200304,
                title: "chanel/香奈尔/香奈尔炫亮魅力唇膏3.5g",
                saler: "lokemick2009",
                shop: "香港我的美丽日记",
                imgUrl: "image/taobao_cart_02.jpg",
                point: 12,
                price: 265,
                num: 1,
                isChecked: false
            }, {
                id: 20200305,
                title: "蝶妆海晳蓝清滢粉底液10#（象牙白）",
                saler: "实体经营",
                shop: "纤巧百媚时尚鞋坊卖家",
                imgUrl: "image/taobao_cart_03.jpg",
                point: 3,
                price: 85,
                num: 1,
                isChecked: true
            }, {
                id: 20200306,
                title: "相宜促销专供 大S推荐 最好用的LilyBell化妆棉",
                saler: "taobao豆豆",
                shop: "红豆豆的小屋",
                imgUrl: "image/taobao_cart_04.jpg",
                point: 12,
                price: 12,
                num: 1,
                isChecked: true
            }
        ],
        // isCheckAll:false
    },
    methods:{
        drop(id){
            let index=this.findIndexByID(id);
            if(this.cartData[index].num>1){
                this.cartData[index].num--;
            }
        },
        add(id){
            let index=this.findIndexByID(id);
            this.cartData[index].num++;
        },
        findIndexByID(id){//根据商品ID 来查找在购物车中索引值
            for(let index in this.cartData){
                if(this.cartData[index].id===id){
                    return index;
                }
            }
        },
        remove(id){
            console.log(id);
            let index=this.findIndexByID(id);
            this.cartData.splice(index,1);
        },
        checkAll(e){//点击事件触发时，还没有更新数据，所有导致数据获取时是之前的状态
            // console.log(this.isCheckAll);
            // 1.解决方案一
            // setTimeout(()=>{
            //     console.log(this.isCheckAll);
            // },0)

            // 2.解决方案二(直接获取当前操作DOM对象上的checked属性)
            // console.log(e.target.checked);
            let isCheckAll=e.target.checked;
            this.cartData.map(item=>item.isChecked=isCheckAll)
        },
        removeCheck(){
                this.cartData=this.cartData.filter(item=>!item.isChecked);
        }
       
        },
        computed:{
            isCheckAll(){//是否全部全选
                let flag=true;
                for(let i=0;i<this.cartData.length;i++){
                    if(!this.cartData[i].isChecked){
                        flag=false;
                        break;
                    }
                }
                return flag;
            },
            totalMoney(){//计算总价
                let total=0;
                this.cartData.filter(item=>item.isChecked).map(item=>total+=(item.price*item.num))
                return total 
            },
            totalPoint(){//计算总积分
                let total=0;
                this.cartData.filter(item=>item.isChecked).map(item=>total+=(item.point*item.num))
                return total 
            },
          
        }
      

})